package com.quickask.idao;

import com.quickask.common.Pager;
import com.quickask.exception.HibernateDaoSupportException;
import com.quickask.common.RowMapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by moonlight on 2015/9/6.
 */
public interface IBaseDao<T, PK extends Serializable> {
    /**
     * 保存实体
     * @param entity
     */
    public void save(T entity);

    /**
     * 保存或者更新实体
     * @param entity
     */
    public void saveOrUpdate(T entity);

    /**
     * 加载实体的load方法
     * @param id 实体的id
     * @return 查找出来的实体
     */
    public T load(PK id);

    /**
     * <get>
     * <查找的get方法>
     * @param id 实体的id
     * @return 查询出来的实体
     */
    public T get(PK id);

    /**
     *
     * @param entity 实体
     * @return 是否包含
     */
    public boolean contains(T entity);

    /**
     * 根据实体删除
     * @param entity
     */
    public void delete(T entity);

    /**
     * 根据id删除实体
     * @param id
     */
    public boolean deleteById(PK id);

    /**
     * 删除实体集合
     * @param entities 实体集合
     */
    public void deleteAll(Collection<T> entities);

    /**
     * <执行Hql语句>
     * @param hqlString hql
     * @param values 不定参数数组
     */
    public int queryHql(String hqlString, Object... values);

    /**
     * <执行Sql语句>
     * @param sqlString sql
     * @param values 不定参数数组
     */
    public void querySql(String sqlString, Object... values);

    /**
     * <根据HQL语句查找唯一实体>
     * @param hqlString HQL语句
     * @param values 不定参数的Object数组
     * @return 查询实体
     */
    public T getByHQL(String hqlString, Object... values);

    /**
     * <根据SQL语句查找唯一实体>
     * @param sqlString SQL语句
     * @param values 不定参数的Object数组
     * @return 查询实体
     */
    public T getBySQL(String sqlString, Object... values);

    /**
     * <根据HQL语句，得到对应的list>
     * @param hqlString HQL语句
     * @param values 不定参数的Object数组
     * @return 查询多个实体的List集合
     */
    public List<T> getListByHQL(String hqlString, Object... values);

    /**
     * <根据SQL语句，得到对应的list>
     * @param sqlString SQL语句
     * @param values 不定参数的Object数组
     * @return 查询多个实体的List集合
     */
    public List<T> getListBySQL(String sqlString, Object... values);

    /**
     * 由sql语句得到List
     * @param sql
     * @param map
     * @param values
     * @return List
     */
    public List findListBySql(final String sql, final RowMapper map, final Object... values);

    /**
     * refresh
     * @param entity
     */
    public void refresh(T entity);

    public void update(T entity);

    public boolean exists(PK id);

    public Integer getRecordCount(Class<T> clazz) throws HibernateDaoSupportException;


    /**
     * <根据HQL得到记录数>
     * @param hqlString HQL语句
     * @param values 不定参数的Object数组
     * @return 记录总数
     */
    public Long countByHql(String hqlString, Object... values);

    /**
     * <HQL分页查询>
     * @param hql HQL语句
     * @param countHql 查询记录条数的HQL语句
     * @param pageNo 下一页
     * @param pageSize 一页总条数
     * @param values 不定Object数组参数
     * @return PageResults的封装类，里面包含了页码的信息以及查询的数据List集合
     */
    public Pager<T> findPagerByFetchedHql(String hql, String countHql, int pageNo, int pageSize, Object... values);

    /**
     * find page object's connection with class
     *
     * @param clazz       according class
     * @param currentPage current page
     * @param pageSize    the number of records per page
     * @return Object's connection
     * @throws com.quickask.exception.HibernateDaoSupportException when accessing and manipulating database happen exception
     */
    public Pager<T> findPager(int currentPage, int pageSize, Class<T> clazz) throws HibernateDaoSupportException;

    /**
     * find page object's connection with hql and param map
     *
     * @param hql         according hql if class param is null
     * @param currentPage current page
     * @param pageSize    the number of records per page
     * @param properties  according param map
     * @return Object's connection
     * @throws HibernateDaoSupportException when accessing and manipulating database happen exception
     */
    public Pager<T> findPager(String hql, int currentPage, int pageSize,
                              Map<String, Object> properties) throws HibernateDaoSupportException;

    public Pager findPagerBySql(String sql, int currentPage, int pageSize,
                                Map<String, Object> properties) throws HibernateDaoSupportException;

    /**
     * The <code>queryList(PK startRecord, PK pageSize)</code> method is query
     * objects according startRecord and pagesize're number, object type is
     * according your implements this method's define type, and implements this
     * interface abstract class must be override all method and inject entity
     * type.
     *
     * @param startRecord Where from the beginning to show this record
     * @param pageSize    The number of records per page
     * @param clazz       according class
     * @return List<T> T is your inject object's type, List is query all object
     * connection
     * @throws HibernateDaoSupportException Throws HibernateDaoSupportException when accessing and
     *                                      manipulating database happen exception.
     */
    public List<T> queryList(int startRecord, int pageSize, Class<T> clazz) throws HibernateDaoSupportException;

    /**
     * find object's connection with hql class and param map
     *
     * @param hql         according hql if class param is null
     * @param startRecord Where from the beginning to show this record
     * @param pageSize    the number of records per page
     * @param clazz       according class
     * @param properties  according param map
     * @return Object's connection
     * @throws HibernateDaoSupportException when accessing and manipulating database happen exception
     */
    public List<T> queryList(String hql, int startRecord, int pageSize, Class<T> clazz,
                             Map<String, Object> properties) throws HibernateDaoSupportException;

}
